package app.dws;

import app.BaseAppV1;
import bean.TradeCourseOrderBean;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import common.Constant;
import function.DimAsyncFunction;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AsyncDataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import util.AtguiguUtil;

import java.math.BigDecimal;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class DwsTradeUserOrder extends BaseAppV1 {
    public static void main(String[] args) {
        new DwsTradeUserOrder().init(4003,
                2,
                "DwsTradeUserOrder",
                Constant.TOPIC_DWD_TRADE_USER_ORDER
        );
    }

    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        // 类别 科目 课程
        // 下单次数 下单人数 下单金额

        // 1. 按照order_detail里的id去重
        SingleOutputStreamOperator<JSONObject> distinguishedStream = distinguishByOrderDetailId(stream);

        // 2. 把数据封装到POJO类中
        SingleOutputStreamOperator<TradeCourseOrderBean> beanStream = parseToPojo(distinguishedStream);

        // 3. 按照courseId(最小粒度)开窗聚合
        SingleOutputStreamOperator<TradeCourseOrderBean> beanStreamWithoutDim = windowAndAgg(beanStream);

        // 4. 补充维度信息（旁路缓存redis，异步IO读出HBase）
        SingleOutputStreamOperator<TradeCourseOrderBean> beanStreamWithDim = addDim(beanStreamWithoutDim);

        // 5. 写出到doris中
        beanStreamWithDim
                .print();
    }

    private SingleOutputStreamOperator<TradeCourseOrderBean> addDim(SingleOutputStreamOperator<TradeCourseOrderBean> beanStreamWithoutDim) {

        // 课程维度
        SingleOutputStreamOperator<TradeCourseOrderBean> courseStream = AsyncDataStream
                .unorderedWait(
                        beanStreamWithoutDim,
                        new DimAsyncFunction<TradeCourseOrderBean>() {
                            @Override
                            public String getTable() {
                                return "dim_course_info";
                            }

                            @Override
                            public String getId(TradeCourseOrderBean input) {
                                return input.getCourseId();
                            }

                            @Override
                            public void addDim(JSONObject dim, TradeCourseOrderBean input) {
                                input.setCourseName(dim.getString("COURSE_NAME"));
                                input.setSubjectId(dim.getString("SUBJECT_ID"));
                            }
                        },
                        60,
                        TimeUnit.SECONDS
                );

        // 学科维度
        SingleOutputStreamOperator<TradeCourseOrderBean> subjectStream = AsyncDataStream
                .unorderedWait(
                        courseStream,
                        new DimAsyncFunction<TradeCourseOrderBean>() {
                            @Override
                            public String getTable() {
                                return "dim_base_subject_info";
                            }

                            @Override
                            public String getId(TradeCourseOrderBean input) {


                                return input.getSubjectId();
                            }

                            @Override
                            public void addDim(JSONObject dim, TradeCourseOrderBean bean) {



                                bean.setSubjectName(dim.getString("SUBJECT_NAME"));
                                bean.setCategoryId(dim.getString("CATEGORY_ID"));


                            }
                        },
                        60,
                        TimeUnit.SECONDS
                );


        // 类别维度
        SingleOutputStreamOperator<TradeCourseOrderBean> categoryStream = AsyncDataStream
                .unorderedWait(
                        subjectStream,
                        new DimAsyncFunction<TradeCourseOrderBean>() {
                            @Override
                            public String getTable() {
                                return "dim_base_category_info";
                            }

                            @Override
                            public String getId(TradeCourseOrderBean input) {


                                return input.getCategoryId();
                            }

                            @Override
                            public void addDim(JSONObject dim, TradeCourseOrderBean bean) {



                                bean.setCategoryName(dim.getString("CATEGORY_NAME"));

                            }
                        },
                        60,
                        TimeUnit.SECONDS
                );
        return categoryStream;
    }

    private SingleOutputStreamOperator<TradeCourseOrderBean> windowAndAgg(SingleOutputStreamOperator<TradeCourseOrderBean> beanStream) {
        return
                beanStream
                .map(json -> {
                    System.out.println(json);
                    return json;
                })
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<TradeCourseOrderBean>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner((bean, ts) -> bean.getTs())
                )
                .keyBy(TradeCourseOrderBean::getCourseId)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(
                        new ReduceFunction<TradeCourseOrderBean>() {
                            @Override
                            public TradeCourseOrderBean reduce(TradeCourseOrderBean bean1,
                                                               TradeCourseOrderBean bean2) throws Exception {
                                bean1.setFinalAmount(bean1.getFinalAmount().add(bean2.getFinalAmount()));
                                return bean1;
                            }
                        },
                        new ProcessWindowFunction<TradeCourseOrderBean, TradeCourseOrderBean, String, TimeWindow>() {
                            @Override
                            public void process(String s,
                                                Context context,
                                                Iterable<TradeCourseOrderBean> iterable,
                                                Collector<TradeCourseOrderBean> collector) throws Exception {
                                TradeCourseOrderBean bean = iterable.iterator().next();

                                bean.setStt(AtguiguUtil.toDateTime(context.window().getStart()));
                                bean.setEdt(AtguiguUtil.toDateTime(context.window().getEnd()));

                                bean.setCurDate(AtguiguUtil.toDate(context.window().getStart()));

                                collector.collect(bean);
                            }
                        }
                );
    }

    private SingleOutputStreamOperator<TradeCourseOrderBean> parseToPojo(SingleOutputStreamOperator<JSONObject> distinguishedStream) {
        return
                distinguishedStream
                .map(new MapFunction<JSONObject, TradeCourseOrderBean>() {
                    @Override
                    public TradeCourseOrderBean map(JSONObject jsonObject) throws Exception {

                        String amount = jsonObject.getString("final_amount");
                        BigDecimal final_amount = new BigDecimal(0);

                        if (amount != null){
                            final_amount= new BigDecimal(amount);
                        }
                        return TradeCourseOrderBean.builder()
                                .courseId(jsonObject.getString("course_id"))
                                .finalAmount(final_amount)
                                .ts(jsonObject.getLong("ts") * 1000)
                                .build();
                    }
                });
    }

    private SingleOutputStreamOperator<JSONObject> distinguishByOrderDetailId(DataStreamSource<String> stream) {

        return
                stream
                        .map(json -> JSON.parseObject(json))
                        .keyBy(obj -> obj.getString("id"))
                        .process(
                                new KeyedProcessFunction<String, JSONObject, JSONObject>() {

                                    private ValueState<JSONObject> maxTsDateState;

                                    @Override
                                    public void open(Configuration parameters) throws Exception {
                                        // 第一条数据存储到状态中
                                        maxTsDateState = getRuntimeContext().getState(new ValueStateDescriptor<JSONObject>("maxTsDateState", JSONObject.class));
                                    }

                                    @Override
                                    public void processElement(JSONObject jsonObject,
                                                               Context context,
                                                               Collector<JSONObject> collector) throws Exception {
                                        if (maxTsDateState.value() == null) {
                                            // 第一条数据来时吗，把数据存到状态中，注册5s的定时器
                                            // 更新 ts最大的那条完成数据去重

                                            maxTsDateState.update(jsonObject);

                                            context.timerService().registerProcessingTimeTimer(
                                                    context.timerService().currentProcessingTime() + 5000L
                                            );
                                        }else {
                                            // 如果不是第一条，则域状态作对比，状态更新为最大的

                                            String last = maxTsDateState.value().getString("row_op_ts");

                                            String current = jsonObject.getString("row_op_ts");

                                            // 判断时间大小
                                            if (AtguiguUtil.isLarger(current,last)){
                                                maxTsDateState.update(jsonObject);
                                            }
                                        }
                                    }

                                    @Override
                                    public void onTimer(long timestamp, OnTimerContext ctx, Collector<JSONObject> out) throws Exception {
                                        // 定时器触发的时候
                                        out.collect(maxTsDateState.value());
                                    }
                                }
                        );
    }
}
